package com.yx.pc.api.controller;

import com.yx.common.utils.PageUtils;
import com.yx.common.utils.Query;
import com.yx.common.utils.R;
import com.yx.modules.api.Dto.TbBookCommentDto;
import com.yx.modules.api.annotation.AuthIgnore;
import com.yx.modules.api.annotation.LoginUser;
import com.yx.modules.api.entity.TbBookComment;
import com.yx.modules.api.entity.TbCommentDianzanRecord;
import com.yx.modules.api.entity.TbUser;
import com.yx.pc.api.Dto.TbCommentDto;
import com.yx.pc.api.service.PcTbBookCommentService;
import com.yx.pc.api.service.PcTbCommentDianzanRecordService;
import com.yx.pc.api.service.PcTdBookService;
import com.yx.system.entity.TdBook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 图书评论
 * 
 * @author luo
 * @email www.baidu.com
 * @date 2018-06-11 14:11:37
 */
@RestController
@RequestMapping("/pc/api/bookComment")
public class PcTbBookCommentController {
	@Autowired
	private PcTbBookCommentService bookCommentService;

	@Autowired
	private PcTdBookService tdBookService;

	@Autowired
	private PcTbCommentDianzanRecordService tbCommentDianzanRecordService;

	/**
	 * 查看所有的书籍评论
	 */
	@AuthIgnore
	@RequestMapping("/list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		//首先查找图书下的所有图书评论
		List<TbBookComment> bookCommentList = bookCommentService.queryList(query);
		int total = bookCommentService.queryTotal(query);
		//初始化数据传输对象集合
		List<TbCommentDto> tbCommentDtoList = new ArrayList<>();
		//循环所有的图书评论
		for (TbBookComment tbBookComment:bookCommentList){
			//初始化数据传输对象
			TbCommentDto tbCommentDto = new TbCommentDto();
			//查询图书评论下的所有回复评论
			List<TbBookComment> tbBookComments = bookCommentService.findByReplyFristId(tbBookComment.getId());
			//注入数据
			tbCommentDto.setTbBookComment(tbBookComment);
			tbCommentDto.setTbBookCommentList(tbBookComments);
			//加入集合
			tbCommentDtoList.add(tbCommentDto);
		}

		PageUtils pageUtil = new PageUtils(tbCommentDtoList, total, query.getLimit(), query.getPage());

		return R.ok().put("data", pageUtil);
	}
	

	/*
	*评论书籍
	*/
	@RequestMapping("/commentBook")
	public R commentBook(@LoginUser TbUser tbUser,Integer bookId,String content){
		if (tbUser!=null){
			TbBookComment bookComment = new TbBookComment();
			bookComment.setUserId(tbUser.getUserId());
			bookComment.setUserName(tbUser.getNickname());
			bookComment.setUserImage(tbUser.getProfileimageurl());
			bookComment.setBookId(bookId);
			bookComment.setContent(content);
			bookComment.setDate(new Date());
			bookComment.setType(0);
			bookComment.setLikeCount(0);
			bookComment.setReplyCount(0);
			bookCommentService.save(bookComment);
			//找到对应图书增加评论数
			TdBook tdBook = tdBookService.selectByPrimaryKey(bookId);
			tdBook.setCommentCount(tdBook.getCommentCount()+1);
			if (tbUser.getSex()){
				tdBook.setCommentBoyCount(tdBook.getCommentBoyCount()+1);
			}else {
				tdBook.setCommentGirlCount(tdBook.getCommentGirlCount()+1);
			}
			//更新图书
			tdBookService.updateByPrimaryKey(tdBook);
		}else {
			return R.error("用户不存在");
		}
		return R.ok();
	}

	/*
	*回复某条评论
	*/
	@RequestMapping("/replyComment")
	public R replyComment(@LoginUser TbUser tbUser,Integer id,String content){
		//根据id去查找评论
		TbBookComment tbBookComment1 = bookCommentService.queryObject(id);
		if (tbBookComment1==null){
			return R.error("评论已经不存在啦");
		}
		//初始化回复评论对象
		TbBookComment tbBookComment = null;
		//如果回复的评论是图书评论
		if (tbBookComment1.getType()==0){
			tbBookComment = new TbBookComment();
			tbBookComment.setUserId(tbUser.getUserId());
			tbBookComment.setUserImage(tbUser.getProfileimageurl());
			tbBookComment.setUserName(tbUser.getNickname());
			tbBookComment.setBookId(tbBookComment1.getBookId());
			tbBookComment.setContent(content);
			tbBookComment.setDate(new Date());
			tbBookComment.setType(1);
			tbBookComment.setReplyId(tbBookComment1.getId());
			tbBookComment.setReplyFristId(tbBookComment1.getId());
			tbBookComment.setReplyUserId(tbBookComment1.getUserId());
			tbBookComment.setReplyUserName(tbBookComment1.getUserName());
			//插入数据
			bookCommentService.save(tbBookComment);
			//增加图书评论的回复数
			tbBookComment1.setReplyCount(tbBookComment1.getReplyCount()+1);
			//更新
			bookCommentService.update(tbBookComment1);
			return R.ok();
		}
		//如果回复的评论是回复评论
		if (tbBookComment1.getType()==1){
			tbBookComment = new TbBookComment();
			tbBookComment.setUserId(tbUser.getUserId());
			tbBookComment.setUserImage(tbUser.getProfileimageurl());
			tbBookComment.setUserName(tbUser.getNickname());
			tbBookComment.setBookId(tbBookComment1.getBookId());
			tbBookComment.setContent(content);
			tbBookComment.setDate(new Date());
			tbBookComment.setType(1);
			tbBookComment.setReplyId(tbBookComment1.getId());
			tbBookComment.setReplyFristId(tbBookComment1.getReplyFristId());
			tbBookComment.setReplyUserId(tbBookComment1.getUserId());
			tbBookComment.setReplyUserName(tbBookComment1.getUserName());
			//插入数据
			bookCommentService.save(tbBookComment);
			return R.ok();
		}
		return R.ok();
	}

	/*
	* 点赞
	*/
	@RequestMapping("/clickPraise")
	public R clickPraise(@LoginUser TbUser tbUser,Integer id){
		//根据id找到评论
		TbBookComment tbBookComment = bookCommentService.queryObject(id);
		if (tbBookComment==null){
			return R.error("查无此评论");
		}
		//判读评论是否图书评论
		if (tbBookComment.getType()==0){
			//查询评论点赞记录
			TbCommentDianzanRecord tbCommentDianzanRecord = tbCommentDianzanRecordService.findBookRcord(tbUser.getUserId(),tbBookComment.getId());
			//如果空，则可以点赞，如果非空进行提示
			if (tbCommentDianzanRecord==null){
				//增加点赞数
				tbBookComment.setLikeCount(tbBookComment.getLikeCount()+1);
				bookCommentService.update(tbBookComment);
				//创建图书评论点赞记录，并保存
				TbCommentDianzanRecord tbCommentDianzanRecord1 = new TbCommentDianzanRecord();
				tbCommentDianzanRecord1.setUserId(tbUser.getUserId());
				tbCommentDianzanRecord1.setCommentId(tbBookComment.getId());
				tbCommentDianzanRecord1.setCreateDate(new Date());
				tbCommentDianzanRecord1.setType(0);
				tbCommentDianzanRecordService.save(tbCommentDianzanRecord1);
				return R.ok();
			}else {
				return R.error("请勿重复点赞");
			}
		}else {
			return R.error("请选择图书评论点赞");
		}
	}

	/*
	* 查看某条图书评论下的回复评论
	*/
	@AuthIgnore
	@RequestMapping("/replyCommentByBookComment")
	public R replyCommentByBookComment(Integer id){
		//根据id找到评论
		TbBookComment tbBookComment = bookCommentService.queryObject(id);
		if (tbBookComment==null){
			return R.error("评论不存在啦");
		}
		//初始化返回集合
		List<TbBookComment> tbBookComments = null;
		//判读评论是否图书评论
		if (tbBookComment.getType()==0){
			tbBookComments = bookCommentService.findByReplyFristId(tbBookComment.getId());
		}else {
			return R.error("请选择图书评论查看");
		}
		return R.ok().put("data",tbBookComments);
	}

	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	public R info(@PathVariable("id") Integer id){
		TbBookComment bookComment = bookCommentService.queryObject(id);
		
		return R.ok().put("bookComment", bookComment);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	public R save(@RequestBody TbBookComment bookComment){
		bookCommentService.save(bookComment);
		
		return R.ok();
	}
	
	/**
	 * 用户删除评论
	 */
	@RequestMapping("/delete")
	public R update(Integer id){
		//首先找到这条图书评论
        TbBookComment tbBookComment = bookCommentService.queryObject(id);
        //必须为图书评论
        if (tbBookComment.getType()==0){
            //根据图书评论找到关联的回复评论
            List<TbBookComment> tbBookComments = bookCommentService.findByReplyFristId(tbBookComment.getId());
            if (tbBookComments.size()!=0){
                //循环删除回复评论
                for (TbBookComment comment:tbBookComments){
                    bookCommentService.delete(comment.getId());
                }
            }
            //删除图书评论
            bookCommentService.delete(tbBookComment.getId());
            return R.ok();
        }else {
            return R.error("请删除图书评论");
        }
	}
	
	/**
	 * 用户查看自己的书评
	 */
	@RequestMapping("/bookCommentByUser")
	public R delete(@LoginUser TbUser tbUser,@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);
		query.put("userId",tbUser.getUserId());

		List<TbBookComment> bookCommentList = bookCommentService.queryList1(query);

		int total = bookCommentService.queryTotal1(query);
		//初始化DTO集合
		List<TbBookCommentDto> tbBookCommentDtos = new ArrayList<>();

		for (TbBookComment tbBookComment:bookCommentList){
			TbBookCommentDto tbBookCommentDto = new TbBookCommentDto();
			//通过图书id查询到对应图书
			TdBook tdBook = tdBookService.selectByPrimaryKey(tbBookComment.getBookId());
			//注入到DTO
			tbBookCommentDto.setTdBook(tdBook);
			tbBookCommentDto.setTbBookComment(tbBookComment);
			//加入到集合
			tbBookCommentDtos.add(tbBookCommentDto);
		}


		PageUtils pageUtil = new PageUtils(tbBookCommentDtos, total, query.getLimit(), query.getPage());

		return R.ok().put("data", pageUtil);
	}


	/*
	*最新书评
	*/
	@AuthIgnore
	@RequestMapping("/newBookComment")
	public R newBookComment(@RequestParam Map<String,Object> params){
		//查询列表数据
		Query query = new Query(params);
		query.put("type","1");

		//初始化书评DTO对象
		List<TbBookCommentDto> tbBookCommentDtos = new ArrayList<>();
		List<TbBookComment> bookCommentList = bookCommentService.queryList1(query);
		int total = bookCommentService.queryTotal1(query);

		for (TbBookComment tbBookComment:bookCommentList){
			//初始化DTO对象
			TbBookCommentDto tbBookCommentDto = new TbBookCommentDto();
			//根据id找到图书
			TdBook tdBook = tdBookService.selectByPrimaryKey(tbBookComment.getBookId());
			//注入到DTO
			tbBookCommentDto.setTdBook(tdBook);
			tbBookCommentDto.setTbBookComment(tbBookComment);
			//加入DTO集合
			tbBookCommentDtos.add(tbBookCommentDto);
		}

		PageUtils pageUtil = new PageUtils(tbBookCommentDtos, total, query.getLimit(), query.getPage());

		return R.ok().put("data", pageUtil);
	}
}
